/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-31 17:23:35
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-01 14:13:52
 */
import bagCtr from "../ctrs/bags/bagCtr";
import gameCtr from "../ctrs/gameCtr";
import netCtr from "../ctrs/netCtr";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { drawScroll2 } from "../libs/core/utils";
import { SceneManager } from "../libs/manager/SceneManager";
import { INotification } from "../libs/pureMvc/Interfaces";
import { gameConfig } from "../shared/game/gameConfig";
const { ccclass, property } = cc._decorator;
@ccclass
export default class djCom extends BaseGameComponent {
  @property({ type: cc.Node, tooltip: "内容节点" })
  contNode: cc.Node = null;

  @property({ type: cc.Prefab, tooltip: "预制体" })
  bagPref: cc.Prefab = null;

  @property({ type: cc.Label, tooltip: "道具描述" })
  djDesc: cc.Label = null;

  @property({ type: cc.ScrollView, tooltip: "滑动" })
  scroll: cc.ScrollView = null;

  public onEnable(): void {
    super.onEnable();
    this.drawUI();
  }

  drawUI() {
    let list = bagCtr.ins.bagArr;
    // console.log(list);
    this.contNode.removeAllChildren();
    drawScroll2(this.contNode, list, this.bagPref, "bagItemCom", 4, 10, 15);
    this.scroll.scrollToTop();
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_BAGCOM_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    let body = notification.getBody();
    switch (evtName) {
      case PureMVCEvents.ON_BAGCOM_UDPATE:
        if (bagCtr.ins.curEny) {
          this.djDesc.string = `道具描述: ${bagCtr.ins.curEny.desc}`;
        } else {
          this.djDesc.string = ``;
        }
        break;
    }
  }

  onUse() {

    if (bagCtr.ins.curEny) {
      netCtr.ins.bagUpdate(bagCtr.ins.curEny.id, 2).then(() => {
        SceneManager.getInstance().pushLayer("tipLayer", bagCtr.ins.curEny.desc);
        switch (bagCtr.ins.curEny.id) {
          case 1:
            bagCtr.ins.hpId = gameCtr.ins.barracksArr[Math.floor(Math.random() * gameCtr.ins.barracksArr.length)].id;
            // console.log(bagCtr.ins.hpId);
            break;
          case 2:
            gameCtr.ins.gold += 200;
            break;
          case 3:
            gameCtr.ins.fireIntel = 901;
            break;
          case 4:
            bagCtr.ins.isSpeedAdd = true;
            break;
          case 5:
            bagCtr.ins.isDelay = true;
            break;
          case 6:
            bagCtr.ins.isGoldAdd = true;
            break;
          case 7:
            bagCtr.ins.offerTime = 6000;
            break;
          case 8:
            bagCtr.ins.isRecover = true;
            bagCtr.ins.recoverTime = 1000;
            break;
        }
        bagCtr.ins.bagArr.forEach((ele, i) => {
          if (ele.id == bagCtr.ins.curEny.id) {
            bagCtr.ins.bagArr.splice(i, 1);
          }
        })
        bagCtr.ins.curEny = null;
        this.drawUI();

        gameCtr.ins.curScene.emit("onDjNode");
      })

    } else {
      SceneManager.getInstance().pushLayer("tipLayer", "未选择道具");
    }

  }
}